﻿using System;
using System.IO;
using System.Reflection;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class Pager
    {
        /// <summary>
        /// 每页数据条数
        /// </summary>
        public int ItemCountPerPage { get; set; } = 10;

        public EventHandler OnFirstPageClicked;
        public EventHandler OnPrePageClicked;
        public EventHandler OnNextPageClicked;
        public EventHandler OnLastPageClicked;
        public EventHandler OnPageIndexChanged;

        public static readonly DependencyProperty PageIndexProperty =
            DependencyProperty.Register(nameof(PageIndex), typeof(int), typeof(Pager));

        public static readonly DependencyProperty TotalPageCountProperty =
            DependencyProperty.Register(nameof(TotalPageCount), typeof(int), typeof(Pager));

        public static readonly DependencyProperty TotalItemCountProperty =
            DependencyProperty.Register(nameof(TotalItemCount), typeof(int), typeof(Pager));

        public int PageIndex
        {

            get => int.Parse(GetValue(PageIndexProperty).ToString());
            set
            {
                SetValue(PageIndexProperty, value);
                Refresh();
            }
        }

        public int TotalPageCount
        {
            get => int.Parse(GetValue(TotalPageCountProperty).ToString());
            set
            {
                SetValue(TotalPageCountProperty, value);
                Refresh();
            }
        }

        public int TotalItemCount
        {
            get => int.Parse(GetValue(TotalItemCountProperty).ToString());
            set
            {
                SetValue(TotalItemCountProperty, value);
                Refresh();
            }
        }

        public Pager()
        {
            InitializeComponent();

            var lang = SystemInfo.GetCurrentLocale();
            lang = lang.Replace("-", "");
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var langRd = Composer<ResourceDictionary>.GetLanguages(path, lang);
            Resources.MergedDictionaries.Add(langRd);
        }

        private void OnFirstClicked(object sender, RoutedEventArgs e)
        {
            PageIndex = 1;
            OnPageIndexChanged?.Invoke(this, null);
            OnFirstPageClicked?.Invoke(this, new EventArgs());
            Refresh();
        }

        private void OnPreviousClicked(object sender, RoutedEventArgs e)
        {
            PageIndex--;
            OnPageIndexChanged?.Invoke(this, null);
            OnPrePageClicked?.Invoke(this, new EventArgs());
            Refresh();
        }

        private void OnNextClicked(object sender, RoutedEventArgs e)
        {
            PageIndex++;
            OnPageIndexChanged?.Invoke(this, null);
            OnNextPageClicked?.Invoke(this, new EventArgs());
            Refresh();
        }

        private void OnLastClicked(object sender, RoutedEventArgs e)
        {
            PageIndex = TotalPageCount;
            OnPageIndexChanged?.Invoke(this, null);
            OnLastPageClicked?.Invoke(this, new EventArgs());
            Refresh();
        }

        private void Refresh()
        {
            btnPrePage.IsEnabled = PageIndex > 1;
            btnFirstPage.IsEnabled = PageIndex > 1;
            btnNextPage.IsEnabled = PageIndex < TotalPageCount;
            btnLastPage.IsEnabled = PageIndex < TotalPageCount;
            lblTotalItemCount.Content = TotalItemCount;
            if (lblTotalItemCount.Content.ToInt() < 0)
            {
                Col1.Width=new GridLength(0);
                Col2.Width = new GridLength(0);
            }
            else
            {
                Col1.Width = new GridLength(1,GridUnitType.Auto);
                Col2.Width = new GridLength(1, GridUnitType.Auto);
            }
        }
    }
}
